package com.xiao.controller;

import com.xiao.domain.ResponseResult;
import com.xiao.domain.dto.AddSysUserDto;
import com.xiao.domain.dto.SaveUpdateSysUserDto;
import com.xiao.domain.dto.UserListDto;
import com.xiao.domain.vo.PageVo;
import com.xiao.enums.AppHttpCodeEnum;
import com.xiao.service.BlogSysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/user")
@Api(tags = "用户")
public class BlogSysUserController {

    @Autowired
    BlogSysUserService blogSysUserService;


    @GetMapping("/list")
    @ApiOperation("获取用户列表")
    @ApiImplicitParams({
                    @ApiImplicitParam(name = "pageNum",value = "页码"),
                    @ApiImplicitParam(name = "pageSize",value = "页面大小"),
                    @ApiImplicitParam(name = "userListDto",value = "搜索内容 DTO")})
    public ResponseResult<PageVo> userList(Integer pageNum, Integer pageSize,UserListDto userListDto){ // @RequestBody 是用于 Application/json 的请求体，而 get请求是 用 & 隔开的请求，不能使用 @RequestBody

        ResponseResult<PageVo> result = blogSysUserService.userList(pageNum, pageSize, userListDto);

        return result;
    }

    @PostMapping
    @ApiOperation("新增用户及其角色关联关系")
    @ApiImplicitParam(name = "addSysUserDto",value = "新增用户，及其角色列表")
    public ResponseResult addSysUser(@RequestBody AddSysUserDto addSysUserDto){
        ResponseResult responseResult = blogSysUserService.addSysUser(addSysUserDto);
        return responseResult;
    }

    @DeleteMapping("/{id}")
    @ApiOperation("根据用户列表ID，删除用户")
    @ApiImplicitParam(name = "id",value = "用户ID列表")
    public ResponseResult delSysUserById(@PathVariable("id") List<Long> ids){

        Boolean flag = blogSysUserService.delSysUserById(ids);

        ResponseResult responseResult;
        if (flag){
            responseResult=ResponseResult.okResult();
        }else {
            responseResult=ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
        }
        return responseResult;
    }

    @GetMapping("/{id}")
    @ApiOperation("修改角色前获取角色角色")
    @ApiImplicitParam(name = "id",value = "角色ID")
    public ResponseResult updateSysUserById(@PathVariable("id") Long id){
        ResponseResult responseResult = blogSysUserService.updateSysUserById(id);
        return  responseResult;
    }

    @PutMapping
    @ApiOperation("保存已经修改好的用户信息，包含用户与角色关系")
    @ApiImplicitParam(name = "saveUpdateSysUserDto",value = "接收角色信息-Dto")
    public ResponseResult saveUpdate(@RequestBody SaveUpdateSysUserDto saveUpdateSysUserDto){
        ResponseResult responseResult = blogSysUserService.saveUpdate(saveUpdateSysUserDto);
        return responseResult;
    }


}
